package org.jeecg.modules.cw.service.impl;

import org.jeecg.modules.cw.entity.CwLxrygzjbxmxbTj;
import org.jeecg.modules.cw.entity.CwLxrygzjbxmxb;
import org.jeecg.modules.cw.mapper.CwLxrygzjbxmxbMapper;
import org.jeecg.modules.cw.mapper.CwLxrygzjbxmxbTjMapper;
import org.jeecg.modules.cw.service.ICwLxrygzjbxmxbTjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 离休人员工资及保险明细提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-05
 * @Version: V1.0
 */
@Service
public class CwLxrygzjbxmxbTjServiceImpl extends ServiceImpl<CwLxrygzjbxmxbTjMapper, CwLxrygzjbxmxbTj> implements ICwLxrygzjbxmxbTjService {

	@Autowired
	private CwLxrygzjbxmxbTjMapper cwLxrygzjbxmxbTjMapper;
	@Autowired
	private CwLxrygzjbxmxbMapper cwLxrygzjbxmxbMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwLxrygzjbxmxbTj cwLxrygzjbxmxbTj, List<CwLxrygzjbxmxb> cwLxrygzjbxmxbList) {
		cwLxrygzjbxmxbTjMapper.insert(cwLxrygzjbxmxbTj);
		if(cwLxrygzjbxmxbList!=null && cwLxrygzjbxmxbList.size()>0) {
			for(CwLxrygzjbxmxb entity:cwLxrygzjbxmxbList) {
				//外键设置
				entity.setMainId(cwLxrygzjbxmxbTj.getId());
				cwLxrygzjbxmxbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwLxrygzjbxmxbTj cwLxrygzjbxmxbTj,List<CwLxrygzjbxmxb> cwLxrygzjbxmxbList) {
		cwLxrygzjbxmxbTjMapper.updateById(cwLxrygzjbxmxbTj);
		
		//1.先删除子表数据
		cwLxrygzjbxmxbMapper.deleteByMainId(cwLxrygzjbxmxbTj.getId());
		
		//2.子表数据重新插入
		if(cwLxrygzjbxmxbList!=null && cwLxrygzjbxmxbList.size()>0) {
			for(CwLxrygzjbxmxb entity:cwLxrygzjbxmxbList) {
				//外键设置
				entity.setMainId(cwLxrygzjbxmxbTj.getId());
				cwLxrygzjbxmxbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwLxrygzjbxmxbMapper.deleteByMainId(id);
		cwLxrygzjbxmxbTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwLxrygzjbxmxbMapper.deleteByMainId(id.toString());
			cwLxrygzjbxmxbTjMapper.deleteById(id);
		}
	}
	
}
